// pages/myAddress/myAddress.js

var app = getApp();
var page = 1;
var selectedNews = false;
var newsList = [];

Page({
  data: {
    listdefault: 1,
    addresslist: [],
    goodsId: [],
    mount: [],
    canCheck: false,
    backType: "",
    gotype: "",
    getchecked: "",
    showpage: false
  },
  //点击切换地址
  checkAddress: function (e) {
    if (this.data.canCheck == true) {
      var index = e.currentTarget.dataset.index;
      var addresslist = this.data.addresslist;
      for (let i = 0; i < addresslist.length; i++) {
        if (addresslist[i].ischecked == true) {
          addresslist[i].ischecked = false;
        }
      }
      addresslist[index].ischecked = true;
      this.setData({
        addresslist: addresslist
      });
      var info = JSON.stringify(addresslist[index]);
      this.setData({
        info: info
      })
      wx.setStorage({
        key: 'addrinfo',
        data: info
      })
      var that = this;
      wx.navigateBack({
        url: '../orderSure/orderSure?info=' + info + '&goodsId=' + that.data.goodsId + '&mount=' + that.data.mount + '&backType=' + this.data.backType + '&spec_price_id=' + that.data.spec_price_id,
      })
    }
  },
  //点击编辑地址
  bindEditTap: function (e) {
    var index = e.currentTarget.dataset.index;
    var info = JSON.stringify(this.data.addresslist[index]);
    this.setData({
      info: info
    })
    wx.setStorage({
      key: 'addrinfo',
      data: info
    })
    if (this.data.listdefault == this.data.addresslist[index].id) {
      var isdefault = 1;
    } else {
      isdefault = 2;
    }

    wx.navigateTo({
      url: '../addAddress/addAddress?info=' + info + '&isdefault=' + isdefault + '&goodsId=' + this.data.goodsId + '&mount=' + this.data.mount + '&backType=' + this.data.backType + '&gotype=' + this.data.gotype + '&spec_price_id=' + this.data.spec_price_id,
    })
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    that.setData({
      backType: options.backType,
      gotype: options.gotype
    })
    if (options.gotype && options.gotype == "person") {
      that.setData({
        canCheck: false
      })
    } else {
      that.setData({
        canCheck: true
      })
    }
    if (!!options.goodsId) {
      that.setData({
        goodsId: options.goodsId,
        mount: options.mount,
        spec_price_id: options.spec_price_id
      })
    }
    if (!!options.getchecked) {
      that.setData({
        getchecked: options.getchecked
      })
    }
    that.getAddressList();

  },
  getAddressList: function () {
    var that = this;
    var url2 = '/address/index';
    var params2 = {
      uid: app.globalData.userInfo.id
    };
    app.HttpGetSend(url2, params2, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
      } else {
        if (res.data) {

          var addresslist = res.data.list;
          for (let i = 0; i < addresslist.length; i++) {
            addresslist[i].ischecked = false;
            if (!!that.data.getchecked) {
              if (addresslist[i].id == that.data.getchecked) {
                addresslist[i].ischecked = true;
                that.setData({
                  info: JSON.stringify(addresslist[i])
                })
                wx.setStorage({
                  key: 'addrinfo',
                  data: JSON.stringify(addresslist[i])
                })
              }
            }
          }
          if (!that.data.getchecked) {
            addresslist[0].ischecked = true;
            that.setData({
              info: JSON.stringify(addresslist[0])
            })
            wx.setStorage({
              key: 'addrinfo',
              data: JSON.stringify(addresslist[0])
            })

          }

          that.setData({
            addresslist: addresslist,
            listdefault: res.data.default,
            showpage: true
          })
        } else {
          that.setData({
            addresslist: [],
            listdefault: '',
            showpage: true
          })
        }
      }
    });
  },
  bindAddTap: function (e) {
    wx.navigateTo({
      url: '../addAddress/addAddress?goodsId=' + this.data.goodsId + '&mount=' + this.data.mount + '&backType=' + this.data.backType + '&gotype=' + this.data.gotype + '&spec_price_id=' + this.data.spec_price_id,
    })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    this.getAddressList();
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭

  },
  // onShareAppMessage: function () {

  // },
  /** 
   * 页面上拉触底事件的处理函数 
   */
  onReachBottom: function () {
    // if (selectedNews == true){
    //   var that = this;
    //   // 显示加载图标  
    //   wx.showLoading({
    //     title: '玩命加载中',
    //   })
    //   // 页数+1  
    //   var url2 = '/api/Index/get_consult';
    //   var params2 = {
    //     token: app.loginfo.token,
    //     type: 2,
    //     page: page,
    //     page_size: 5
    //   };
    //   app.HttpPostSend(url2, params2, function (res) {
    //     var _this = this;
    //     if (res.message != "success") {
    //       app.showTipMsg(res.message);
    //     } else {
    //       page += 1;
    //       for (var i = 0; i < res.data.news_consult.length; i++) {
    //         newsList.push(res.data.news_consult[i]);
    //       }
    //       that.setData({
    //         newsList: newsList
    //       });
    //       // 隐藏加载框  
    //       wx.hideLoading();
    //     }
    //   });

    // }else{
    //   wx.hideLoading();
    // }
  }
})